<?php

namespace App\Exports;

use App\Models\finance\ReceivableTemplate2Model;
use App\Models\TakeOrderModel;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ReceivableTemplate2Export implements FromCollection, WithHeadings, WithMapping
{
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function collection()
    {
        $query = ReceivableTemplate2Model::with(['customer', 'take_order', 'create_admin']);
        
        // 筛选条件
        if ($this->request->filled('take_order_id')) {
            $query->where('take_order_id', $this->request->input('take_order_id'));
        }
        
        if ($this->request->filled('customer_id')) {
            $query->where('customer_id', $this->request->input('customer_id'));
        }
        
        if ($this->request->filled('flight_number')) {
            $query->where('flight_number', 'like', '%' . $this->request->input('flight_number') . '%');
        }
        
        if ($this->request->filled('departure')) {
            $query->where('departure', 'like', '%' . $this->request->input('departure') . '%');
        }
        
        if ($this->request->filled('destination')) {
            $query->where('destination', 'like', '%' . $this->request->input('destination') . '%');
        }
        
        if ($this->request->filled('status')) {
            $query->where('status', $this->request->input('status'));
        }
        
        if ($this->request->filled('currency')) {
            $query->where('currency', $this->request->input('currency'));
        }
        
        if ($this->request->filled('flight_date_start') && $this->request->filled('flight_date_end')) {
            $query->whereBetween('flight_date', [$this->request->input('flight_date_start'), $this->request->input('flight_date_end')]);
        }
        
        if ($this->request->filled('date_start') && $this->request->filled('date_end')) {
            $query->whereBetween('created_at', [$this->request->input('date_start'), $this->request->input('date_end') . ' 23:59:59']);
        }

        if ($this->request->filled('take_order_sn')) {
            $takeOrderIds = TakeOrderModel::where('sn', 'like', '%' . $this->request->input('take_order_sn') . '%')
                ->pluck('id')
                ->toArray();
            if (!empty($takeOrderIds)) {
                $query->whereIn('take_order_id', $takeOrderIds);
            }
        }

        return $query->orderBy('id', 'desc')->get();
    }

    public function headings(): array
    {
        return [
            'ID',
            '提单号',
            '客户名称',
            '航班日期',
            '航班号',
            '始发地',
            '目的地',
            '件数',
            '毛重',
            '体积重',
            '计费重',
            '提货费',
            '操作费',
            '报关费',
            '主单文件费',
            '总额',
            '币种',
            '状态',
            '创建人',
            '创建时间',
        ];
    }

    public function map($item): array
    {
        $statusMap = [
            1 => '未付款',
            2 => '部分付款',
            3 => '已付款',
        ];

        return [
            $item->id,
            $item->take_order?->sn ?? '',
            $item->customer?->name ?? '',
            $item->flight_date,
            $item->flight_number,
            $item->departure,
            $item->destination,
            $item->pieces,
            $item->gross_weight,
            $item->volume_weight,
            $item->chargeable_weight,
            $item->pick_up_fee,
            $item->handling_charge,
            $item->customs_declaration_fee,
            $item->mawb_documents_fee,
            $item->total,
            $item->currency,
            $statusMap[$item->status] ?? '未知',
            $item->create_admin?->name ?? '',
            $item->created_at->format('Y-m-d H:i:s'),
        ];
    }
} 